from common.Ulitys import ForumDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time


class ForumDM:
    driver=""
    url=""
    def __init__(self):
        self.driver=ForumDriver.driver
    
    # 帖子详情页私信
    def PostDetailsDM(self):
        self.driver.find_element(By.CSS_SELECTOR,"#nav_board_index > a").click()
        time.sleep(1)
        # 找到第一个帖子
        self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a").click()
        time.sleep(1)
        # 给作者私信
        #btn_details_send_message
        self.driver.find_element(By.CSS_SELECTOR,"#btn_details_send_message").click()
        time.sleep(1)
        # 输入私信内容
        #index_message_receive_content
        self.driver.find_element(By.CSS_SELECTOR,"#index_message_receive_content").send_keys("你好，我是测试用户")
        time.sleep(1)
        # 发送私信
        #btn_index_send_message
        self.driver.find_element(By.CSS_SELECTOR,"#btn_index_send_message").click()
        time.sleep(1)
        # 检查是否发送成功
        text=self.driver.find_element(By.CSS_SELECTOR,"body > div.jq-toast-wrap.bottom-right > div > h2").text
        assert text=="成功"
        ForumDriver.getScreenShot()
        
    # 回复私信
    def ReplyDM(self):
        # 回到主页
        self.driver.find_element(By.CSS_SELECTOR,"#nav_board_index > a").click()
        # 点击私信按钮
        WebDriverWait(self.driver,5).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div > a")))
        self.driver.find_element(By.CSS_SELECTOR,"body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div > a").click()
        time.sleep(1)
        
        # 点击第一个未读私信
        self.driver.find_element(By.CSS_SELECTOR,"#index_div_message_list > div:nth-child(1) > div > div.col.text-truncate > a").click()
        time.sleep(1)
        
        # 点击回复按钮 #btn_index_message_reply
        self.driver.find_element(By.CSS_SELECTOR,"#btn_index_message_reply").click()
        time.sleep(1)
        
        # 编辑回复内容 #index_message_reply_receive_content
        self.driver.find_element(By.CSS_SELECTOR,"#index_message_reply_receive_content").send_keys("生活愉快哦")
        time.sleep(1)
        
        # 点击发送 #btn_index_send_message_reply
        self.driver.find_element(By.CSS_SELECTOR,"#btn_index_send_message_reply").click()
        
        time.sleep(1)
        # 提示发送成功 /html/body/div[4]/div/text()
        text=self.driver.find_element(By.CSS_SELECTOR,"body > div.jq-toast-wrap.bottom-right > div > h2").text
        assert text=="成功"
        ForumDriver.getScreenShot()
        
        # 关闭私信页面
        self.driver.find_element(By.CSS_SELECTOR,"#index_message_offcanvasEnd > div.offcanvas-header > button").click()
        time.sleep(1)
        
        
        
        
        
